package org.linlinjava.litemall.wx.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.dao.LitemallCollectMapper;
import org.linlinjava.litemall.db.domain.LitemallCollect;
import org.linlinjava.litemall.db.domain.LitemallCollectAccessory;
import org.linlinjava.litemall.db.domain.LitemallGoods;
import org.linlinjava.litemall.db.domain.LitemallGoodsSpecification;
import org.linlinjava.litemall.db.service.LitemallCollectAccessoryService;
import org.linlinjava.litemall.db.service.LitemallCollectService;
import org.linlinjava.litemall.db.service.LitemallGoodsService;
import org.linlinjava.litemall.db.service.LitemallGoodsSpecificationService;
import org.linlinjava.litemall.wx.dto.WxCollectDTO;
import org.linlinjava.litemall.wx.vo.CollectVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ：stephen
 * @date ：Created in 2020/6/9 18:43
 * @description：TODO
 */
@Service
@RequiredArgsConstructor
public class WxCollectService {

    private final LitemallCollectService collectService;

    private final LitemallCollectMapper collectMapper;

    private final LitemallGoodsService litemallGoodsService;

    private final LitemallCollectAccessoryService collectAccessoryService;

    private final LitemallGoodsSpecificationService litemallGoodsSpecificationService;

    /**
     * 添加或更新收藏
     * @param userId
     * @param wxCollectDto
     * @return
     */
    public Object addorupdate(Integer userId, WxCollectDTO wxCollectDto) {
        LitemallCollect collect = collectService.queryByGoodsId(userId, wxCollectDto.getGoodsId());

        if (collect != null) {
            BeanUtils.copyProperties(wxCollectDto, collect);
            collectAccessoryService.deleteByCollectId(collect.getId());
            if(wxCollectDto.getAccessories().size() != 0){
                for(LitemallCollectAccessory accessory : wxCollectDto.getAccessories()){
                    accessory.setCollectId(collect.getId());
                    collectAccessoryService.add(accessory);
                }
            }
            collectService.updateById(collect);
        } else {
            collect = new LitemallCollect();
            BeanUtils.copyProperties(wxCollectDto, collect);
            collect.setUserId(userId);
            collectService.add(collect);
            if(wxCollectDto.getAccessories().size()!=0){
                for(LitemallCollectAccessory accessory : wxCollectDto.getAccessories()){
                    accessory.setCollectId(collect.getId());
                    collectAccessoryService.add(accessory);
                }
            }
        }
        return ResponseUtil.ok();
    }

    /**
     * 删除收藏
     * @param userId
     * @param goodsId
     * @return
     */
    public Object delete(Integer userId, Integer goodsId) {
        LitemallCollect litemallCollect = collectService.queryByGoodsId(userId, goodsId);
        if(litemallCollect != null){
            collectService.deleteByGoodsId(userId, goodsId);
            collectAccessoryService.deleteByCollectId(litemallCollect.getId());
        }
        return ResponseUtil.ok();
    }

    public Object isCollect(Integer userId,Integer goodsId){
        LitemallCollect collect =   collectService.queryByGoodsId(userId,goodsId);
        CollectVo vo = new CollectVo();
        if(collect != null){
            vo.setIsCollect(true);
            return ResponseUtil.ok(vo);

        }else{
            vo.setIsCollect(false);
            return ResponseUtil.ok(vo);
        }
    }

    /**
     * 用户收藏列表
     *
     * @param userId 用户id
     * @return {@link ResponseUtil.Response}
     */
    public ResponseUtil.Response userCollect(Integer userId) {
        //获得所有收藏商品ID
        List<LitemallCollect> litemallCollects = collectMapper.selectList(Wrappers.lambdaQuery(LitemallCollect.class)
                .select(LitemallCollect::getGoodsId)
                .eq(LitemallCollect::getUserId, userId)
        );
        List<LitemallCollect> litemallCollectList = litemallCollects.stream().distinct().map(litemallCollect -> collectService.queryByGoodsId(userId, litemallCollect.getGoodsId())).collect(Collectors.toList());
        //返回商品集合
        List<LitemallGoods> litemallGoodsList= CollUtil.newArrayList();
        litemallCollectList.stream().forEach(litemallCollect->{
            if(ObjectUtil.isNotNull(litemallCollect)){
                //如果收藏的商品被删除则提示
                LitemallGoods litemallGoods = litemallGoodsService.findById(litemallCollect.getGoodsId());
                if(ObjectUtil.isNotNull(litemallGoods)){
                    LitemallGoodsSpecification goodsSpecification = litemallGoodsSpecificationService.findById(litemallCollect.getSpecificationIds()[0]);
                    litemallGoods.setRetailPrice(goodsSpecification.getPrice());
                    litemallGoodsList.add(litemallGoods);
                }
            }
        });
        return ResponseUtil.ok(litemallGoodsList);
    }
}
